package com.gym.chatroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;

/**
 * 客户端的线程类，多客户的线程类，服务器接收多用户请求的线程类
 * @author admin
 *
 */
@SuppressWarnings("all")
public class ClientThread extends Thread {
	//监听客户端
	private Socket socket;
	//当前客户端的读写对象
	private BufferedReader reader;
	//发送的消息
	private PrintWriter write;
	//当前用户的名字
	private String name;
	private String ip;
	public PrintWriter getWrite() {
		return write;
	}
	public void setWrite(PrintWriter write) {
		this.write = write;
	}
	
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public ClientThread(Socket socket) {
		//初始化参数
		this.socket = socket;
		try {
			//字节流转换为字符流
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
			//字符输出流
			write = new PrintWriter(socket.getOutputStream());
			//用户注册，接收用户的基本信息
			this.name = reader.readLine();
			this.ip = socket.getInetAddress().getHostAddress();
			//通知用户上线
			sendMessages("ADD#"+this.name+this.ip);
			//用户列表
			Server.clients.put(name+ip,this);
			//服务器返回自己的信息
			write.println("INFO#"+this.name+this.ip);
			write.flush();
			//给当前用户发送用户列表LIST#user1IP#user2IP...
			ClientThread clientThread = null;
			StringBuffer stringBuffer = new StringBuffer("LIST");
			for(String key :Server.clients.keySet()){
				//遍历所有的客户端线程
				clientThread =Server.clients.get(key);
				stringBuffer.append("#"+clientThread.name+clientThread.ip);
			}
			write.println(stringBuffer);
			write.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 提前建立好规则，是私聊还是群发，等
	 */
	@Override
	public void run() {//专门用来接收客户端消息进行处理
		String mseeage = null;
		String [] mes = null;
		while (true) {
			try {
				mseeage = reader.readLine();
				mes = mseeage.split("#");
				System.err.println(mseeage);
				//用户下线DEL#nameIP
				if(mes[0].equals("DEL")){
					Server.clients.remove(mes[1]);
					System.err.println(Server.clients.size());
					sendMessages(mseeage);
					return;
					//私聊PRI#NAMEIP#message
				}else if(mes[0].equals("PRI")){
					ClientThread target =Server.clients.get(mes[1]);
					target.getWrite().println("【"+this.name+this.ip+"】"+"对你说:"+mes[2]);
					target.getWrite().flush();
				}else if(mes[0].equals("ALL")){
					//将消息转发
					sendMessages(mes[1]);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	/**
	 * 发送消息
	 * @param messages
	 */
	public void sendMessages(String messages){
		ClientThread clientThread;
		for(String key :Server.clients.keySet()){
			//遍历所有的客户端线程
			clientThread = Server.clients.get(key);
			try {
				clientThread.getWrite().println(messages);
				clientThread.getWrite().flush();
			} catch (Exception e) {
				//发送失败，移除
				Server.clients.remove(clientThread);
			}
			
		}
	}
	
}
